<%
'#################################################################################
'## UnWritten Open Source Blog Engine
'#################################################################################
'## Copyright (C) 2008 Luigi Violin
'##
'## This program is free software: you can redistribute it and/or modify
'## it under the terms of the GNU General Public License as published by
'## the Free Software Foundation, either version 2 of the License, or
'## (at your option) any later version.
'##
'## This program is distributed in the hope that it will be useful,
'## but WITHOUT ANY WARRANTY; without even the implied warranty of
'## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'## GNU General Public License for more details.
'##
'## You should have received a copy of the GNU General Public License
'## along with this program.  If not, see <http://www.gnu.org/licenses/>.
'##
'## You can obtain support from our forums at:
'##     <http://www.unwrittenblog.com/forums/>
'## Contact the author directly:
'##     <zaamit@hotmail.com/>
'##
'#################################################################################
%>
<%
function gen_content()
select case page(ent_type)
 case 1
 	gen_content = gen_post()
 case 2
 	gen_content = gen_article()
 case 3
 	gen_content = page(content)
 case else
 	gen_content = gen_article()
end select
end function

function gen_post
	gen_post = getTemplatePage("post.asp")
	gen_post = replace(gen_post,"[#-POST_TITLE-#]",page(title))
	gen_post = replace(gen_post,"[#-POST_CONTENT-#]",page(content))
	dbRS2.open "select * from " & db_prefix & "posts," & db_prefix & "users where author = " & db_prefix & "users.id and " & db_prefix & "posts.id = " & page(id),dbConn
	gen_post = replace(gen_post,"[#-POST_META-#]",gen_meta())
	gen_post = replace(gen_post,"[#-USER_AVATAR-#]",get_avatar(dbRS2("author")))
	dbRS2.close
	'COMMENTS FORM & LIST
	gen_post = replace(gen_post,"[#-POST_COMMENTS-#]",gen_comments())
	if Session("u_level") = 10 then
		gen_post = replace(gen_post,"[#-ADMIN_TOOLS-#]","<p align=""right""><a href=""/admin/edit.asp?id=" & page(id) & """>EDIT</a></p>")
			else
				gen_post = replace(gen_post,"[#-ADMIN_TOOLS-#]","")
	end if
end function

function gen_article
	gen_article = getTemplatePage("article.asp")
	gen_article = replace(gen_article,"[#-ARTICLE_TITLE-#]",page(title))
	gen_article = replace(gen_article,"[#-ARTICLE_CONTENT-#]",page(content))
	if session("u_level") = 10 then
		gen_article = replace(gen_article,"[#-ADMIN_TOOLS-#]","<p align=""right""><a href=""/admin/edit.asp?id=" & page(id) & """>EDIT</a></p>")
		else
		gen_article = replace(gen_article,"[#-ADMIN_TOOLS-#]","")
	end if
end function

function gen_comments()
	gen_comments = "<a name=""comments""></a><br /><strong><font size=""14px"" color=""#c8c8c8"">Comments</font></strong><br /><br /><br />" & vbcrlf
	gen_comments = gen_comments & "<div id=""comments_div"">"
'comments list
	dbRS2.open "select * from " & db_prefix & "comments where postid = " & page(id) & " and status = 1",dbConn
	Dim comm_code, real_comm_code
	if not dbRS2.eof then comm_code = getTemplatePage("comment.asp")
	do while not dbRS2.eof
		get_user(dbRS2("author_id"))
		if dbRS2("author_id") = 0 then
			'comment author was a guest
			user(nick) = dbRS2("author_name")
			user(email) = dbRS2("author_email")
			user(url) = dbRS2("author_url")
			user(ip) = dbRS2("author_ip")
			user(level) = 0
		end if
		real_comm_code = "<div id=""show_comment"
		if user(level) = 10 then real_comm_code = real_comm_code & "_admin"
		real_comm_code = real_comm_code & """><div id=""inner"">"
		real_comm_code = real_comm_code & comm_code
		if user(url) <> "" then
			real_comm_code = replace(real_comm_code,"[#-COMMENT_AUTHOR_LINK-#]","<a href=""" & user(url) & """ target=""_blank"">" & user(nick) & "</a>")
			else
				real_comm_code = replace(real_comm_code,"[#-COMMENT_AUTHOR_LINK-#]",user(nick))
		end if
		real_comm_code = replace(real_comm_code,"[#-COMMENT_DATE-#]",dat2rss(dbRS2("date_gmt"),"UTC"))
		real_comm_code = replace(real_comm_code,"[#-COMMENT_TEXT-#]",unbad(dbRS2("text")))
		if session("u_level") = 10 then
			real_comm_code = replace(real_comm_code,"[#-ADMIN_TOOLS-#]","&nbsp;&nbsp;(<a href=""/admin/comment-edit.asp?id=" & dbRS2("id") & """>EDIT</a>)")
			else
				real_comm_code = replace(real_comm_code,"[#-ADMIN_TOOLS-#]","")
		end if
		real_comm_code = replace(real_comm_code,"[#-USER_AVATAR-#]",get_avatar(dbRS2("author_id")))
		gen_comments = gen_comments & real_comm_code & "</div></div>" & vbcrlf
		dbRS2.movenext
	loop
	dbRS2.close
	gen_comments = gen_comments & "</div>"
'comment form
	gen_comments = gen_comments & "<form method=""post"" id=""comment_form"" name=""comment_form"" action=""/includes/dopostcomment.asp"">"
  if Session("u_level") > 0 then
    gen_comments = gen_comments & "Logged in as: <strong>" & Session("u_name") & "</strong><br />"
	gen_comments = gen_comments & "<input type=""hidden"" name=""author_id"" value=""" & Session("u_id") & """ />"
	gen_comments = gen_comments & "<input type=""hidden"" name=""author_name"" value=""" & Session("u_name") & """ />"
	gen_comments = gen_comments & "<input type=""hidden"" name=""author_url"" value=""" & Session("u_url") & """ />"
	gen_comments = gen_comments & "<input type=""hidden"" name=""author_email"" value=""" & Session("u_email") & """ />"
   else
	gen_comments = gen_comments & "<strong>Name</strong><br /><input type=""text"" name=""author_name"" class=""comment_field""><br />"
	gen_comments = gen_comments & "<strong>E-Mail</strong><br /><input type=""text"" name=""author_email"" class=""comment_field""><br />"
	gen_comments = gen_comments & "<strong>Url</strong><br /><input type=""text"" name=""author_url"" class=""comment_field""><br />"
	gen_comments = gen_comments & "<input type=""hidden"" name=""author_id"" value=""0"" />"
  end if
  	gen_comments = gen_comments & "<br /><textarea id=""elm1"" name=""text"" rows=""5"" cols=""80"" style=""width:340px;""></textarea>"
	gen_comments = gen_comments & "<input type=""hidden"" name=""postid"" value=""" & page(id) & """ />"
	gen_comments = gen_comments & "<input type=""hidden"" name=""return_url"" value=""" & "/?" & request.QueryString & "#comments"">" & vbcrlf
	gen_comments = gen_comments & "<div class=""form_buttons""><br /><input type=""submit"" value=""Post Comment"" class=""form_button"" tabindex=""3"" /></div>" & vbcrlf
	gen_comments = gen_comments & "</form>"
end function

function gen_meta()
	gen_meta = "Posted on <i>" & dat2rss(dbRS2("pdate_gmt"),"UTC") & "</i> &nbsp;by <strong><a href=""/user.asp?name=" & dbRS2("nick") & """>" & dbRS2("nick") & "</a></strong>. <a href=""/?pg=" & dbRS2("ent_name") & "#comments"">Comments: " & getPostComments(dbRS2(0)) & "</a>.<br>Tagged as: " & getPostTags(dbRS2(0))
end function

function unbad(badstr)
	unbad = badstr
	dbRS3.open "select * from " & db_prefix & "badwords",dbConn
	do while not dbRS3.eof
		unbad = replace(unbad,dbRS3("text"),dbRS3("replace_text"))
		dbRS3.movenext
	loop
	dbRS3.close
end function
%>